<?php
	/**
	* Products Module Datatype
	* @author trungdt
	* @since Sep 27, 2011
	* 
	*/
	class productsmodule_categories {
		function product_form($object, $config) {
			global $user;

			if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
			exponent_forms_initialize();
			
			// check permission, for some fields for the admin
			$loc=unserialize($config->location_data);
			// permission checking
			$have_permission=false;
			if (exponent_users_isAdmin() || exponent_permissions_check('administrate',$loc) || exponent_permissions_check('create',$loc) || exponent_permissions_check('edit',$loc))
			{
				$have_permission=true;
			}
			
			$lang=exponent_lang_loadFile("modules/productsmodule/common.php");


			$form = new form();
			if (!isset($object->id)) {
				$object->title = '';
				$object->seo_name='';
				$object->search_des = '';
				$object->cat_id = productsmodule::GetFirstCategoryId();
				$object->post_date=time();                                           $object->og_thumbnail=0; // og thumbnail image file path
				$object->best_seller=0;
				$object->images="";
				$object->description="";
				$object->spec="";
				$object->brand_id=0;
				$object->sku='';
				$object->price=0;
				$object->price_orginal=0;
				$object->product_state=0;
				$object->user_id=$user->id;
				$object->videos="";
				$object->stars=1;
				$object->product_state2=0;
				$object->on_sale=0;
				$object->hot_product=0;
				$object->stock_remain=0;
			} else {
				$form->meta('id',$object->id);
			}

			$form->register('title','Product Name',new textcontrol($object->title));
			$form->register('seo_name','Seo Name',new textcontrol($object->seo_name));
			$form->register("",null,new htmlcontrol("<p>Leave SEO field blank for automatic generate SEO name</p>"));
			$form->register('search_des','Search Description',new texteditorcontrol($object->search_des,3,90));

			$form->register('og_thumbnail','Thumbnail', new ajax_select_file('og_thumbnail',0,$object->og_thumbnail,true));
			
			// price
			$form->register("price", $lang['product_price'], new textcontrol($object->price, 60, false, 20, "","number"));
			
			//$object->cat_id = productsmodule::GetFirstCategoryId();
			$form->register("description", "Product Description", new htmleditorcontrol($object->description));
			$form->register("spec", "Product Specification", new htmleditorcontrol($object->spec));
			
			$form->register("best_seller", "Best Selling", new checkboxcontrol($object->best_seller,true));
			$form->register("on_sale", "On Sale", new checkboxcontrol($object->on_sale,true));
			$form->register("hot_product", "This is a hot product (show hot product icon)?", new checkboxcontrol($object->hot_product,true));
			$form->register("stock_remain", "How many items remain in your stock?", new textcontrol($object->stock_remain, 50, false, 10, "","digits"));
			
			// rating stars
			$stars=array();
			for ($i=0;$i<=5;$i++)
				$stars[$i]=str_repeat("*",$i+1);
			$form->register('stars',$lang['product_stars'],new dropdowncontrol($object->stars,$stars));
			
			// product state
			$product_state=array();
			for ($i=0;$i<$lang['product_state_count'];$i++)
			{
				$product_state[$i]=$lang["product_state{$i}"];
			}

			$form->register('product_state',$lang['product_state'],new dropdowncontrol($object->product_state,$product_state));
			
			// product state2
			$product_state2=array();
			for ($i=0;$i<$lang['product_state_2_count'];$i++)
			{
				$product_state2[$i]=$lang["product_state_2_{$i}"];
			}

			$form->register('product_state2',$lang['product_state_2'],new dropdowncontrol($object->product_state2,$product_state2));
			
			//$object->images="";
			
			//$object->brand_id=0;
			//$object->sku='';
			/*$object->price=0;
			$object->=0;
			$object->user_id=$user->id;
			$object->videos="";

*/
			
			if ($have_permission)
			{
				$form->register("",null, new htmlcontrol("<h1>{$lang['data_modeler_forAdminOnly']}</h1>"));
				// price buy
				$form->register("price_orginal", $lang['product_price_orginal'], new textcontrol($object->price_orginal, 60, false, 20, "","number"));
				
				 
			}
			$form->register('submit','',new buttongroupcontrol('Save','','Cancel'));
			return $form;
		}
		
		/**
		* Product Update to Modeler data
		* 
		* @author trungdt
		* @since Oct 3, 2011
		* @param mixed $values
		* @param mixed $object
		*/
		function product_update($values,$object, $loc) {
			$have_permission=false;
			if (exponent_users_isAdmin() || exponent_permissions_check(array('administrate', "create"),$loc))
			{
				$have_permission=true;
			}
			
			// normal update
			$object->title = $values['title'];
			$object->seo_name=$values['seo_name'];
			if (trim($object->seo_name) == "")
				$object->seo_name = _to_vietnamese_with_nomark($values['title']);
			else
				$object->seo_name = _to_vietnamese_with_nomark($values['seo_name']);
			
			$object->search_des = " ".$values['search_des']." ";
			$object->cat_id = intval($values['cat_id']);
			
			$object->og_thumbnail=intval($values['og_thumbnail']); // og thumbnail image file path
			$object->images=$values['images'];
			$object->description=$values['description'];
			$object->spec=$values['spec'];
			// for easy search
			$object->search_description=" ".strip_tags($values['description'])." ";
			$object->search_spec=" ".strip_tags($values['spec'])." ";
			
			$object->brand_id=intval($values['brand_id']);
			$object->weight=intval($values['weight']);
			
			$object->videos=$values['videos'];
			$object->accessories=$values['accessories'];
			
			$object->stars=floatval($values['stars']);
			$object->product_state2=intval($values['product_state2']);
			$object->on_sale=(isset($values['on_sale'])?1:0);
			$object->hot_product=(isset($values['hot_product'])?1:0);
			$object->best_seller=(isset($values['best_seller'])?1:0);
			$object->stock_remain=intval($values['stock_remain']);
			
			// if post new or have permission on updating
			if ($have_permission || !isset($object->id))
			{
				$object->price_orginal=floatval($values['price_orginal']);
				$object->price=floatval($values['price']);
				$object->price_sale=floatval($values['price_sale']);
				$object->product_state=intval($values['product_state']);
				$object->public_state=intval($values['public_state']);
				$object->sku=intval($values['sku']);
				$object->rfid=intval($values['rfid']);
				
				// for auto generate sku and rfid
				if ($object->sku == 0)
				{
					$object->sku=str_pad(rand(),10,"0",STR_PAD_LEFT);
				}
				else
				{
					$temp="{$object->sku}";
					if (strlen($temp)>10)
						$temp=substr($temp,0,10);
					else
						$temp=str_pad($temp,10,"0",STR_PAD_LEFT);
					$object->sku=$temp;
				}
				if ($object->rfid == 0)
				{
					// maximum length is 10 digits
					$object->rfid=$object->sku;
				}
				else
				{
					$temp="{$object->rfid}";
					if (strlen($temp)>10)
						$temp=substr($temp,0,10);
					else
						$temp=str_pad($temp,10,"0",STR_PAD_LEFT);
					$object->rfid=$temp;
				}
				
				
				
				// if add new, not allow update this properties
				if (!isset($object->id)){
					$object->user_id=$user->id;
					$object->post_date = time();
				}
			}
			return $object;
		}



		function brand_form($object, $config) {
			if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
			exponent_forms_initialize();

			$form = new form();
			if (!isset($object->id)) {
				$object->title = '';
				$object->seo_name = '';
				$object->search_des = '';
				$object->logo_id = 0;
			} else {
				$form->meta('id',$object->id);
			}

			$form->register('title','Brand Name',new textcontrol($object->title));
			$form->register('seo_name','Seo Name',new textcontrol($object->seo_name));
			$form->register("",null,new htmlcontrol("<p>Leave SEO field blank for automatic generate SEO name</p>"));
			$form->register('search_des','Search Description',new texteditorcontrol($object->search_des,3,90));
			$form->register('logo_id','Logo', new ajax_select_file('logo_id',0,$object->logo_id,true));
			$form->register('submit','',new buttongroupcontrol('Save','','Cancel'));
			return $form;
		}


		function brand_update($values,$object) {
			$object->title = $values['title'];
			$object->seo_name = $values['seo_name'];
			$object->logo_id = intval($values['logo_id']);
			if (trim($object->seo_name) == "")
				$object->seo_name = _to_vietnamese_with_nomark($values['title']);
			else
				$object->seo_name = _to_vietnamese_with_nomark($values['seo_name']);
			$object->search_des = $values['search_des'];
			return $object;
		}


		function categories_form($object, $parent, $config) {
			if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
			exponent_forms_initialize();

			$form = new form();
			if (!isset($object->id)) {
				$object->title = '';
				$object->seo_name = '';
				$object->search_des = '';
				$object->parent_id=0;
				$object->file_id=0;
				$object->container_top_show=1;
				$object->container_top_usediff=0;
				$object->container_bottom_show=1;
			} else {
				$form->meta('id',$object->id);
			}

			$form->meta('parent_id',$parent);

			$form->register('title','Categories Title',new textcontrol($object->title));
			$form->register('seo_name','Seo Name',new textcontrol($object->seo_name));
			$form->register("",null,new htmlcontrol("<p>Leave SEO field blank for automatic generate SEO name</p>"));
			$form->register('search_des','Search Description',new texteditorcontrol($object->search_des,3,90));
			$form->register('file_id','Logo file', new ajax_select_file('file_id',0,$object->file_id,true));
			//
			$form->register('container_top_show','Show container on top',new checkboxcontrol($object->container_top_show, true));
			$form->register('container_top_usediff','Make new container on top, dont use common container',new checkboxcontrol($object->container_top_usediff,true));
			$form->register('container_bottom_show','Show container on bottom',new checkboxcontrol($object->container_bottom_show, true));
			
			
				
			$form->register('submit','',new buttongroupcontrol('Save','','Cancel'));
			return $form;
		}

		function categories_update($values,$object) {
			$object->title = $values['title'];
			$object->seo_name = $values['seo_name'];
			if (trim($object->seo_name) == "")
				$object->seo_name = _to_vietnamese_with_nomark($values['title']);
			else
				$object->seo_name = _to_vietnamese_with_nomark($values['seo_name']);
			$object->search_des = $values['search_des'];
			$object->parent_id = intval($values['parent_id']);
			$object->file_id = intval($values['file_id']);
			//
			$object->container_top_show=(isset($values['container_top_show'])?1:0);
			$object->container_top_usediff=(isset($values['container_top_usediff'])?1:0);
			$object->container_bottom_show=(isset($values['container_bottom_show'])?1:0);
			return $object;
		}
		
		/**
		* Currency Form
		* 
		* @param mixed $object
		* @param mixed $config
		* @return form
		*/
		function currency_form($object) {
			$lang=exponent_lang_loadFile("modules/productsmodule/common.php");
			
			if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
			exponent_forms_initialize();

			$form = new form();
			if (!isset($object->id)) {
				$object->name = '';
				$object->rate= 1;
				$object->sign= '';
				$object->sign_position= 0;
				$object->locale= '';
				$object->flag_id= 0;
				$object->precision= 0;
				$object->dec_point= ',';
				$object->decimal_point= '.';
			} else {
				$form->meta('id',$object->id);
			}

			$sign_position=array();
			$sign_position[0]=$lang['currency_sign_position_no_display'];
			$sign_position[1]=$lang['currency_sign_position_left'];
			$sign_position[2]=$lang['currency_sign_position_right'];
			
			$form->register('name',$lang['currency_name'],new textcontrol($object->name,35,false, 100,"",true));
			$form->register('rate',$lang['currency_rate'],new textcontrol($object->rate,35, false, 10,"","number"));
			$form->register("", null, new htmlcontrol($lang['currency_rate_description']));
			$form->register('sign',$lang['currency_sign'],new textcontrol($object->sign,3,false, 3,"",true));
			$form->register('sign_position',$lang['currency_sign_position'], new dropdowncontrol($object->sign_position, $sign_position));
			$form->register('locale',$lang['currency_locale'],new textcontrol($object->locale,35,false,10));
			$form->register('precision',$lang['currency_precision'],new textcontrol($object->precision,35,false,2,"","digits"));
			 $form->register("", null, new htmlcontrol($lang['currency_precision_des']));
			 $form->register('decimal_point',$lang['currency_decimal_point'],new textcontrol($object->decimal_point,35,false, 1,"",true));
			 $form->register('dec_point',$lang['currency_dec_point'],new textcontrol($object->dec_point,35,false, 1,"", true));
			$form->register('flag_id',$lang['currency_flag'], new ajax_select_file('flag_id',0,$object->flag_id,true));
			
			$form->register('submit','',new buttongroupcontrol('Save','','Cancel'));
			return $form;
		}

		/**
		* Currency Update
		* 
		* @param mixed $values
		* @param mixed $object
		*/
		function currency_update($values,$object) {
			$object->name = $values['name'];
			$object->rate = floatval($values['rate']);
			$object->sign = $values['sign'];
			$object->sign_position = intval($values['sign_position']);
			$object->flag_id = intval($values['flag_id']);
			$object->locale = $values['locale'];
			$object->precision = $values['precision'];
			$object->dec_point = $values['dec_point'];
			$object->decimal_point = $values['decimal_point'];
			return $object;
		}
		
		/**
		* Video Form
		* 
		* @param mixed $object
		* @param mixed $config
		* @return form
		*/
		function video_form($object, $config) {
			if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
			exponent_forms_initialize();
			
			global $db;
			
			$cats=$db->selectDropdown("productsmodule_video_cat","title");
			
			$form = new form();
			if (!isset($object->id)) {
				$object->title = '';
				$object->title2 = '';
				$object->seo_name = '';
				$object->search_des = '';
				$object->video_type = 0;
				$object->file_id= 0;
				$object->thumbnail= 0;
				$object->youtube_embed= "";
				$object->use_own_thumbnail= 0;
				foreach ($cats as $key=>$value) {
					$object->cat_id=$key;
					break;
				}
				$object->prior= 0;
				$object->highlighted= 0;
			} else {
				$form->meta('id',$object->id);
			}

			$form->register('title','Video Title',new textcontrol($object->title));
			$form->register('title2','Second video title',new textcontrol($object->title2));
			$form->register('seo_name','Seo Name',new textcontrol($object->seo_name));
			$form->register("",null,new htmlcontrol("<p>Leave SEO field blank for automatic generate SEO name</p>"));
			$form->register('search_des','Search Description',new texteditorcontrol($object->search_des,3,90));
			
			$video_type=array();
			$video_type[0]="Video upload on server";
			$video_type[1]="MP3 File";
			$video_type[2]="Embeded code";
			$form->register('video_type',"Video Type",new dropdowncontrol($object->video_type,$video_type));
			
			$form->register('file_id','Select file', new ajax_select_file('file_id',0,$object->file_id,true));
			$form->register('thumbnail','Select Thumbnail', new ajax_select_file('thumbnail',0,$object->thumbnail,false));
			
			$form->register('youtube_embed','Embed Code',new texteditorcontrol($object->youtube_embed,3,90,false));
			$form->register('use_own_thumbnail','Force use uploaded thumbnail', new checkboxcontrol($object->use_own_thumbnail));
			$form->register('prior','This is prior video', new checkboxcontrol($object->prior));
			$form->register('highlighted','Highlighted', new checkboxcontrol($object->highlighted));
			
			$form->register('cat_id','Category',new dropdowncontrol($object->cat_id, $cats));
			
			
			$form->register('submit','',new buttongroupcontrol('Save','','Cancel'));
			return $form;
		}


		/**
		* Video Update
		* 
		* @param mixed $values
		* @param mixed $object
		*/
		function video_update($values,$object) {
			$object->title = $values['title'];
			$object->title2 = $values['title2'];
			$object->seo_name = $values['seo_name'];
			if (trim($object->seo_name) == "")
				$object->seo_name = _to_vietnamese_with_nomark($values['title']);
			else
				$object->seo_name = _to_vietnamese_with_nomark($values['seo_name']);
			
			$object->seo_name=_ab_check_seoname("productsmodule_video",$object->seo_name, $object->id);
			$object->search_des = $values['search_des'];
			
			
			$object->video_type = intval($values['video_type']);
			$object->file_id= intval($values['file_id']);
			$object->thumbnail= intval($values['thumbnail']);
			$object->youtube_embed= $values['youtube_embed'];
			$object->use_own_thumbnail= isset($values['use_own_thumbnail'])?1:0;
			$object->highlighted= isset($values['highlighted'])?1:0;
			$object->prior= isset($values['prior'])?1:0;
			$object->cat_id =intval($values['cat_id']);
			
			if (!isset($object->id))
				$object->postdate=aboid_getCurrentTime();
			
			return $object;
		}

	}
?>
